package customControls;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jaden.washer.*;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.util.List;

import activity.OrderPage;
import activity.WasherPage;
import order.Order;
import washer.Washer;

public class OrderConfirmedAdapter extends ArrayAdapter<Order>implements View.OnClickListener {

    Order order;
    int Position;
    private int resourceId;
    MyApplication application;
    private static final String TAG = "OrderConfirmedAdapter";
    public OrderConfirmedAdapter(@NonNull Context context, int resource, List<Order> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }
    public class ViewHolder
    {
        TextView orderID;
        TextView washerID;
        TextView price;
        TextView number;
        TextView orderstatus;
        Button detailinfo;
        Button orderpay;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        Position=position;
        //根据position 得到当前子项的Commodity实例
        order=getItem(position);
        ViewHolder viewHolder = null;
        if(convertView==null)
        {
            Log.d(TAG, "getView: "+position);
            //为当前子项加载指定布局->resourceId
            convertView=LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            viewHolder=new ViewHolder();
            //获取控件实例
            getInstance(viewHolder,convertView);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        setClickListener(viewHolder);
        //显示控件信息
        showInformation(viewHolder);
        return convertView;
    }

    //获取控件实例
    private void getInstance(ViewHolder viewHolder, View view) {
        viewHolder.orderID=(TextView)view.findViewById(R.id.textView_orderID_orderlistviewitemcomfirmed);
        viewHolder.washerID=(TextView)view.findViewById(R.id.textView_washerID_orderlistviewitemcomfirmed);
        viewHolder. price=(TextView)view.findViewById(R.id.textView_price_orderlistviewitemcomfirmed);
        viewHolder.number=(TextView)view.findViewById(R.id.textView_number_orderlistviewitemcomfirmed);
        viewHolder.orderstatus=(TextView)view.findViewById(R.id.textView_orderstatus_orderlistviewitemcomfirmed);
        viewHolder.detailinfo=(Button)view.findViewById(R.id.button_detailinfo_orderlistviewitemcomfirmed);
        viewHolder.orderpay=(Button)view.findViewById(R.id.button_pay_orderlistviewitemcomfirmed);
        application=MyApplication.getInstance();
    }
    //显示控件信息
    private void showInformation(ViewHolder viewHolder) {
        viewHolder.orderID.setText(viewHolder.orderID.getText()+String.valueOf(order.getOrderID()));
        viewHolder.washerID.setText(viewHolder.washerID.getText()+String.valueOf(order.getWasherID()));
        viewHolder.price.setText(viewHolder.price.getText()+String.valueOf(order.getOrderMoney()));
        viewHolder.number.setText(viewHolder.number.getText()+String.valueOf(Position));
        if(order.getOrderStatus()==4)
            viewHolder.orderstatus.setText("已完成");
        else if(order.getOrderStatus()==3)
            viewHolder.orderstatus.setText("正在洗衣");
        else
            viewHolder.orderstatus.setText("预约中");
        if(order.getOrderStatus()==2)
        {
            viewHolder.orderpay.setVisibility(View.VISIBLE);
            viewHolder.detailinfo.setVisibility(View.INVISIBLE);
        }
        else
        {
            viewHolder.orderpay.setVisibility(View.INVISIBLE);
            viewHolder.detailinfo.setVisibility(View.VISIBLE);
        }
    }
    private void setClickListener(ViewHolder viewHolder) {
        viewHolder.detailinfo.setOnClickListener(this);
        viewHolder.orderpay.setOnClickListener(this);
    }

    private void dialogShow()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View v = inflater.inflate(R.layout.order_detail_dialog, null);
        TextView ordernumber=(TextView)v.findViewById(R.id.textView_orderID_orderdetaildialog);
        TextView orderstatus=(TextView)v.findViewById(R.id.textView_orderstatus_orderdetaildialog);
        TextView ordermoney=(TextView)v.findViewById(R.id.textView_price__orderdetaildialog);
        TextView createtime=(TextView)v.findViewById(R.id.textView_createtime_orderdetaildialog);
        TextView confirmtime=(TextView)v.findViewById(R.id.textView_confirmtime_orderdetaildialog);
        TextView paytime=(TextView)v.findViewById(R.id.textView_paytime_orderdetaildialog);
        TextView finishtime=(TextView)v.findViewById(R.id.textView_finishtime_orderdetaildialog);
        TextView washID=(TextView)v.findViewById(R.id.textView_washerID_orderdetaildialog);
        TextView washpattern=(TextView)v.findViewById(R.id.textView_washpattern_orderdetaildialog);
        Button confirm=(Button)v.findViewById(R.id.button_confirm_orderdetaildialog);

        ordernumber.setText(ordernumber.getText()+String.valueOf(order.getOrderID()));
        if(order.getOrderStatus()==3)
            orderstatus.setText("正在洗衣");
        else
            orderstatus.setText("已完成");
        ordermoney.setText(String.valueOf(order.getOrderMoney())+ordermoney.getText());
        createtime.setText(createtime.getText()+String.valueOf(order.getCreateTime()));
        confirmtime.setText(confirm.getText()+String.valueOf(order.getConfirmTime()));

        //
        paytime.setVisibility(View.VISIBLE);
        finishtime.setVisibility(View.VISIBLE);

        if(order.getPayedTime()==null)
        {
            paytime.setVisibility(View.INVISIBLE);
            finishtime.setVisibility(View.INVISIBLE);
        }
        else
        {
            paytime.setText(paytime.getText()+order.getPayedTime().toString());
            if(order.getFinishedTime()==null)
                finishtime.setVisibility(View.INVISIBLE);
            else
                finishtime.setText(finishtime.getText()+order.getFinishedTime().toString());
        }
        if(order.getOrderPattern()==1)
            washpattern.setText("正常模式");
        else
            washpattern.setText("快洗模式");
        washID.setText("洗衣机ID:"+String.valueOf(order.getWasherID()));


        final Dialog dialog=builder.create();
        dialog.show();
        dialog.getWindow().setContentView(v);
        dialog.getWindow().setGravity(Gravity.BOTTOM);

       confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
    });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_detailinfo_orderlistviewitemcomfirmed:
               /* Intent intent=new Intent(getContext(), OrderDetailInformationPage.class);
                intent.putExtra("position",Position);
                getContext().startActivity(intent);
                */
                dialogShow();
                break;
            case R.id.button_pay_orderlistviewitemcomfirmed:
                String url="http://47.96.159.236:8080/Wash/Order?method=StudentPay";
                //打包Json数据
                JSONObject userJson=new JSONObject();
                try {
                    userJson.put("StuID",order.getStuID());
                    userJson.put("OrderID",order.getOrderID());
                    String messageJson=String.valueOf(userJson);
                    TransferData.OrderPay(url,messageJson);
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                while (true) {
                    if(application.isPayOk()) {
                        application.setPayOk(false);
                        ResponseMessage responseMessage=application.getResponseMessage();
                       OrderPage orderPage= OrderPage.getApp();
                       orderPage.getOrder(2);
                        break;
                    }
                }
                break;
        }
    }
}
